//
//  UIBarButtonItem+HYExtension.m
//  (First By Myself)
//
//  Created by Ezra on 16/5/28.
//  Copyright © 2016年 Ezra. All rights reserved.
//

#import "UIBarButtonItem+HYExtension.h"

@implementation UIBarButtonItem (HYExtension)


//正常状态的UIBarButtonItem
+ (instancetype)itemWithImage:(UIImage *)image highImage:(UIImage *)highImage addTarget:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:highImage forState:UIControlStateHighlighted];
    
    [button sizeToFit];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIView *containView = [[UIView alloc]initWithFrame:button.bounds];
    [containView addSubview:button];
    
    return [[self alloc]initWithCustomView:containView];
}

//选中状态的UIBarButtonItem
+ (instancetype)itemWithImage:(UIImage *)image selImage:(UIImage *)selImage addTarget:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:selImage forState:UIControlStateSelected];
    
    [button sizeToFit];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIView *containView = [[UIView alloc]initWithFrame:button.bounds];
    [containView addSubview:button];

    return [[self alloc]initWithCustomView:containView];
}

//返回按钮的UIBarButtonItem

+ (instancetype)itemWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleHighColor:(UIColor *)titleHighColor image:(UIImage *)image selImage:(UIImage *)selImage  addTarget:(id)target action:(SEL)action
{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:title forState:UIControlStateNormal];
    [backButton setTitleColor:titleColor forState:UIControlStateNormal];
    [backButton setTitleColor:titleHighColor forState:UIControlStateHighlighted];
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton setImage:selImage forState:UIControlStateHighlighted];
    [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [backButton sizeToFit];
    
    UIView *contain = [[UIView alloc]initWithFrame:backButton.bounds];
    
    [contain addSubview:backButton];
    
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    
    return [[UIBarButtonItem alloc]initWithCustomView:contain];
    
}

@end
